/**
 * 
 */
package com.finalist.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.finalist.dao.InvoiceTitleDao;
import com.finalist.entity.InvoiceTitle;
import com.finalist.entity.InvoiceTitle.Content;
import com.finalist.entity.InvoiceTitle.InvoiceOpenMethod;
import com.finalist.entity.InvoiceTitle.InvoiceType;
import com.finalist.entity.InvoiceTitle.SelectStatus;
import com.finalist.service.InvoiceTitleService;

/**
 * service-发票台头信息
 * @author pengyb
 *
 */
@Service("invoiceTitleServiceImpl")
public class InvoiceTitleServiceImpl extends BaseServiceImpl<InvoiceTitle, Long> implements InvoiceTitleService
{
    @Resource(name = "invoiceTitleDaoImpl")
    private InvoiceTitleDao invoiceTitleDao;

    @Resource(name = "invoiceTitleDaoImpl")
    public void setBaseDao(InvoiceTitleDao invoiceTitleDao)
    {
        super.setBaseDao(invoiceTitleDao);
    }

    @Override
    public List<InvoiceTitle> findByUserId(Long memberId)
    {
        return invoiceTitleDao.findByUserId(memberId);
    }

    @Override
    public void updateSelectStatus(Long memberId)
    {
        invoiceTitleDao.updateSelectStatus(memberId);
    }

    @Override
    public void updateSelectStatusById(Long id, SelectStatus selectStatus)
    {
        invoiceTitleDao.updateSelectStatusById(id, selectStatus);
    }

    @Override
    public void saveOrUpdateInfo(Long id, String invoiceType, String title, String content, String invoiceOpenMethod, Long memberId)
    {
        InvoiceTitle invoiceTitle = new InvoiceTitle();

        invoiceTitle.setTitle(title);
        invoiceTitle.setId(id);
        if (StringUtils.isBlank(invoiceTitle.getTitle()) && invoiceTitle.getId() != null)
        {//发票抬头为空，则为选择某抬头
            invoiceTitleDao.updateSelectStatus(memberId);//把原先选择的发票抬头变更为未选择
            invoiceTitleDao.updateSelectStatusById(invoiceTitle.getId(), SelectStatus.select);
        }
        else
        {
            Content contentType = Content.detail;
            if ("detail".equals(content))
            {//明细
                contentType = Content.detail;
            }
            else if ("office".equals(content))
            {//办公用品
                contentType = Content.office;
            }
            else if ("computer".equals(content))
            {//电脑配件
                contentType = Content.computer;
            }
            else if ("consume".equals(content))
            {//耗材
                contentType = Content.consume;
            }

            InvoiceType type = InvoiceType.person;
            if (!"person".equals(invoiceType))
            {
                type = InvoiceType.company;
            }
            InvoiceOpenMethod method = InvoiceOpenMethod.general;
            if ("generalElec".equals(invoiceOpenMethod))
            {
                method = InvoiceOpenMethod.generalElec;
            }
            else if ("valueAdd".equals(invoiceOpenMethod))
            {
                method = InvoiceOpenMethod.valueAdd;
            }

            invoiceTitle.setInvoiceType(type);
            invoiceTitle.setContent(contentType);
            invoiceTitle.setInvoiceOpenMethod(method);
            invoiceTitle.setMemberId(memberId);
            invoiceTitle.setSelectStatus(SelectStatus.select);
            //新增时需判断抬头是否已经存在，如果存在则只修改状态

            if (invoiceTitle.getId() != null)
            {//非新增发票抬头，编辑发票抬头

                InvoiceTitle editInvoiceTitle = invoiceTitleDao.find(invoiceTitle.getId());
                if (editInvoiceTitle != null)
                {//判断新增的发票是否存在,存在则不新增发票抬头，只是修改发票抬头状态和相关信息
                    invoiceTitle.setCreateDate(editInvoiceTitle.getCreateDate());
                    invoiceTitle.setModifyDate(new Date());
                    invoiceTitle.setId(editInvoiceTitle.getId());
                }
            }
            else
            {//新增
                InvoiceTitle tempInvoiceTitle = invoiceTitleDao.findByTypeAndTitle(memberId, invoiceTitle.getInvoiceType(), invoiceTitle.getTitle());
                if (tempInvoiceTitle != null)
                {//新增的数据已经存在，修改新增数据的选择状态
                    invoiceTitle.setCreateDate(tempInvoiceTitle.getCreateDate());
                    invoiceTitle.setModifyDate(new Date());
                    invoiceTitle.setId(tempInvoiceTitle.getId());
                }
            }
            invoiceTitleDao.updateSelectStatus(memberId);//把原先选择的发票抬头变更为未选择
            invoiceTitleDao.merge(invoiceTitle);
        }

    }

}
